package jsu.blogger.controller;

import jakarta.servlet.http.HttpServletRequest;
import jsu.blogger.pojo.Post;
import jsu.blogger.pojo.Result;
import jsu.blogger.pojo.Users;
import jsu.blogger.service.inter.SearchService;
import jsu.blogger.util.ReturnResultUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

@RestController
public class SearchController {
    @Autowired
    private SearchService searchService;
    @PostMapping("/searchPost")
    public Result searchPost(HttpServletRequest request){

        Result result= ReturnResultUtil.failure("搜索失败");
        Map<String,Object> map = (Map<String, Object>) request.getAttribute("requestBody");
        String searchContent= (String) map.get("searchContent");
        int currentPage= (int) map.get("currentPage");
        int pageSize= (int) map.get("pageSize");
        System.out.println("正在搜索博客:"+searchContent);
//        System.out.println(searchContent);
//        System.out.println(currentPage);
//        System.out.println(pageSize);
        List<Post> re=searchService.searchPostPage(searchContent,currentPage,pageSize);
        System.out.println("re"+re);
        if(re!=null){
            result=ReturnResultUtil.success(re);
        }
        return result;
    }

    @PostMapping("/searchUser")
    public Result searchUser(HttpServletRequest request){
        Result result= ReturnResultUtil.failure("搜索失败");
        Map<String,Object> map = (Map<String, Object>) request.getAttribute("requestBody");
        String searchContent= (String) map.get("searchContent");
        int currentPage= (int) map.get("currentPage");
        int pageSize= (int) map.get("pageSize");

        List<Users> re=searchService.searchUserPage(searchContent,currentPage,pageSize);
        if(re!=null){
            result=ReturnResultUtil.success(re);
        }
        return result;
    }
}
